<template>
  <page-header-wrapper>
    <a-card :bordered="false" style="margin-bottom: 11px">
      <div class="table-page-search-wrapper" v-if="true">
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col :md="4" :sm="24">
              <a-form-item style="margin: 0">
                <a-input placeholder="请输入卡券名称" v-model="queryParam.search_key" >
                    <a-icon slot="suffix" type="search" />
                </a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" style="margin: 0">
                <a-button style="margin-right: 8px" @click="resetQueryParams">重置</a-button>
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="float: right">
        <a-button type="primary" @click="onAdd" icon="plus">新建</a-button>
      </div>
      <a-divider />
      <s-table rowKey="id" size="default" :columns="columns" :data="loadData" ref="table">
        <div slot="detail" slot-scope="text, item">
          <div style="float: left; height: 40px"><img height="100%" :src="item.pic_url" /></div>
          <div style="line-height: 30px; float: left; padding-left: 20px">{{ item.content }}</div>
        </div>
        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="handleEdit(record)" size="small">编辑</a-button>
          <a-divider type="vertical" />
          <a-popconfirm title="确认删除该卡券吗？" @confirm="handleDelete(record)">
            <a-button type="link" size="small">删除</a-button>
          </a-popconfirm>
        </span>
      </s-table>
      <a-modal
        :title="isNew ? '创建卡券' : '编辑卡券'"
        :width="800"
        :confirm-loading="modalLoading"
        :mask-closable="false"
        :destroy-on-close="true"
        :bodyStyle="{ height: '600px', overflow: 'auto' }"
        v-model="visible"
        @ok="handleOk"
      >
        <a-form class="permission-form" :form="form">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" style="display: none" label="主键" hasFeedback>
            <a-input placeholder="主键" :disabled="!isNew" v-decorator="['id']" />
          </a-form-item>

          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="卡券名称" hasFeedback>
            <a-input
              placeholder="卡券名称"
              extra="在商品编辑--选择卡券时显示"
              v-decorator="['name', { rules: [{ required: true, message: '请填写卡券名称' }] }]"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="卡券图片">
            <storage-select
              selectNum="1"
              type="image"
              :defaultValue="form_pic_url"
              @onOk="
                (data) => {
                  this.form_pic_url = data
                }
              "
            >
            </storage-select>
            <!-- <a-input
              placeholder="卡券图片"
              allow-clear
              v-decorator="['pic_url', { rules: [{required: true, message: '请填写卡券图片链接'}], getValueFromEvent: (e) => {
                this.form_pic_url = e.target.value
                return e.target.value
              } }]"
            >
              <a-icon
                slot="addonAfter"
                type="upload"
              />

            </a-input>
            <img
              width="100px"
              :src="form_pic_url"
              v-if="form_pic_url"
              alt=""
            /> -->
          </a-form-item>
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="卡券描述"
            extra="用于线下营销
卡券会在用户付款后，自动发放给用户"
          >
            <a-textarea
              :rows="2"
              id="content"
              v-decorator="['content', { rules: [{ required: true, message: '请输入卡券描述' }] }]"
            />
          </a-form-item>
        </a-form>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import pick from 'lodash.pick'
import { STable } from '@/components'
import { cardCouponList, cardCouponDel, cardCouponAdd, cardCouponEdit } from '@/api/marketing'

const STATUS = {
  1: '显示',
  0: '不显示',
}

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '卡券名称',
    dataIndex: 'name',
  },
  {
    title: '卡券信息',
    dataIndex: 'detail',
    scopedSlots: {
      customRender: 'detail',
    },
  },
  {
    title: '添加时间',
    dataIndex: 'add_time',
  },
  {
    title: '操作',
    width: '150px',
    dataIndex: 'action',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  name: 'ArticleList',
  components: {
    STable,
  },
  data() {
    return {
      isNew: false,
      visible: false,
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 5,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 16,
        },
      },
      form: this.$form.createForm(this),
      // 表头
      columns,
      modalLoading: false,
      statusSelectValue: {
        key: '1',
        label: '显示',
      },
      type_list: [],
      form_pic_url: '',
      // 查询条件参数
      queryParam: {
        search_key: null,
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        Object.assign(parameter, this.queryParam)
        return cardCouponList(parameter)
          .then((res) => {
            console.log('cardCouponList', res)
            this.type_list = res.data.type_list
            return res.data
          })
          .catch((err) => {
            this.$message.error(err.message)
            this.$refs.table.localLoading = false
          })
      },
    }
  },
  filters: {
    statusFilter(key) {
      return STATUS[key]
    },
  },
  created() {},
  methods: {
    changeShow(item) {
      console.log(item)
      const hide = this.$message.loading('请求中...', 0)
      cardCouponEdit({
        key: item.id,
        is_show: item.is_show,
      })
        .then((res) => {
          hide()
          if (res.code === 0) {
            this.$message.success('操作成功！')
            this.$refs.table.refresh()
          } else {
            this.$error({
              content: res.msg,
            })
          }
        })
        .catch((err) => {
          this.$message.error(err.message)
          this.modalLoading = false
        })
    },
    handleEdit(record) {
      this.isNew = false
      this.visible = true
      this.form_pic_url = record.pic_url
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(record, ['id', 'name', 'pic_url', 'content']))
      })
    },
    handleOk(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (err) {
          return false
        }
        Object.assign(values, {
          pic_url: this.form_pic_url,
        })
        this.modalLoading = true
        if (this.isNew) {
          cardCouponAdd(values)
            .then((res) => {
              console.log(res)
              this.modalLoading = false
              if (res.code !== 0) {
                this.$error({
                  content: res.msg,
                })
              } else {
                this.$message.success('卡券创建成功！')
                this.visible = false
                this.$refs.table.refresh()
              }
            })
            .catch((err) => {
              this.$message.error(err.message)
              this.modalLoading = false
            })
        } else {
          cardCouponEdit(values)
            .then((res) => {
              console.log(res)
              this.modalLoading = false
              if (res.code !== 0) {
                this.$error({
                  content: res.msg,
                })
              } else {
                this.$message.success('卡券编辑成功！')
                this.visible = false
                this.$refs.table.refresh()
              }
            })
            .catch((err) => {
              this.$message.error(err.message)
              this.modalLoading = false
            })
        }
      })
    },
    onAdd: function () {
      this.isNew = true
      this.visible = true
      this.form_pic_url = ''
    },
    handleDelete: function (record) {
      const hide = this.$message.loading('删除中...', 0)
      cardCouponDel({
        key: record.id,
      })
        .then((res) => {
          hide()
          if (res.code === 0) {
            this.$message.success('卡券删除成功！')
            this.$refs.table.refresh()
          } else {
            this.$error({
              content: res.msg,
            })
          }
        })
        .catch((err) => {
          this.$message.error(err.message)
          this.modalLoading = false
        })
    },
    resetQueryParams() {
      Object.assign(this.queryParam, {
        search_key: null,
      })
      this.$refs.table.refresh(true)
    },
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
</style>
